package com.lianxian.xindong.old.fragment.home.dynamic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.flyjingfish.openimagelib.OpenImage;
import com.flyjingfish.openimagelib.beans.OpenImageUrl;
import com.flyjingfish.openimagelib.enums.MediaType;
import com.flyjingfish.openimagelib.listener.SourceImageViewIdGet;
import com.lianxian.xindong.old.R;
import com.lianxian.xindong.old.fragment.home.dynamic.endities.DynamicData;
import com.lianxian.xindong.old.fragment.home.selffile.adapter.utils.GlideUtil;

import java.util.List;

public class DynamicRecyclerviewAdapter extends RecyclerView.Adapter {
    List<DynamicData> list;
    Context context;
    LayoutInflater layoutInflater;
    public DynamicRecyclerviewAdapter(List<DynamicData> list, Context context) {
        this.list = list;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_dynamic, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {
        DynamicData data = list.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.commentCountTV.setText(data.getCommentCount()+"");
        viewHolder.likeCountTV.setText(data.getLikeCount()+"");
        GridAdapter adapter = new GridAdapter(context,data.getPicUrls());
        viewHolder.picsGV.setAdapter(adapter);

        viewHolder.picsGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                OpenImage.with(context)
                        //点击ImageView所在的RecyclerView（也支持设置setClickViewPager2，setClickViewPager，setClickGridView，setClickListView，setClickImageView，setClickWebView，setNoneClickView）
                        .setClickGridView(viewHolder.picsGV,new SourceImageViewIdGet() {
                            @Override
                            public int getImageViewId(OpenImageUrl data, int pos) {
                                return R.id.grid_imageview;//点击的ImageView的Id或者切换图片后对应的ImageView的Id
                            }
                        })
                        //点击的ImageView的ScaleType类型（如果设置不对，打开的动画效果将是错误的）
                        .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP,true)
                        //RecyclerView的数据
                        .setImageUrlList(data.getPicUrls(), MediaType.IMAGE)
                        //点击的ImageView所在数据的位置
                        .setClickPosition(pos)
                        //开始展示大图
                        .show();
            }
        });
        viewHolder.wordsTV.setText(data.getWrods());
        viewHolder.hobbiesTV.setText(data.getHobbies());
        viewHolder.addressTV.setText(data.getAddress());
        GlideUtil.GlideWithPlaceHolder(context, data.getHeadPicUrl()).into(viewHolder.headPicIV);
        viewHolder.nicNameTV.setText(data.getNicName());
        viewHolder.ageTV.setText(data.getAge()+"");
        if (data.isGender()){
            viewHolder.genderAndAgeLayout.setBackgroundResource(R.drawable.man_background);
            viewHolder.genderIV.setImageResource(R.mipmap.mengbanzu_148);
        }else {
            viewHolder.genderAndAgeLayout.setBackgroundResource(R.drawable.woman_background);
            viewHolder.genderIV.setImageResource(R.mipmap.mengbanzu_149);

        }
        if (data.isLikeOrNot()){
            viewHolder.likeOrNotIv.setImageResource(R.mipmap.path_24);
        }else {
            viewHolder.likeOrNotIv.setImageResource(R.mipmap.path_53);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView headPicIV;
        private TextView nicNameTV;
        private TextView addressTV;
        private TextView occupationTV;
        private TextView hobbiesTV;
        private TextView wordsTV;
        private TextView dateTimeTV;
        private GridView picsGV;
        private TextView likeCountTV;
        private TextView commentCountTV;
        private ImageView likeOrNotIv;
        private LinearLayout genderAndAgeLayout;
        private TextView ageTV;
        private ImageView genderIV;

        public ViewHolder(View itemView) {
            super(itemView);
            headPicIV = itemView.findViewById(R.id.head_pic);
            nicNameTV = itemView.findViewById(R.id.nic_name);
            addressTV = itemView.findViewById(R.id.address);
            occupationTV = itemView.findViewById(R.id.occupation);
            hobbiesTV = itemView.findViewById(R.id.hobby_string);
            wordsTV = itemView.findViewById(R.id.words);
            dateTimeTV = itemView.findViewById(R.id.date_time);
            picsGV = itemView.findViewById(R.id.grid_image);
            likeCountTV = itemView.findViewById(R.id.like_count);
            commentCountTV = itemView.findViewById(R.id.comment_count);
            likeOrNotIv = itemView.findViewById(R.id.like_image);
            genderAndAgeLayout = itemView.findViewById(R.id.age_gender_layout);
            ageTV = itemView.findViewById(R.id.age);
            genderIV = itemView.findViewById(R.id.gender_image);
        }
    }
}
